/*
*/
import * as enums from './status-enums.js';
// import Vue from 'vue';

const state = {
    // containers: {},
    mapGrid: {},
    openedBoxes: [],
    toggledBoxes: [],

    selectedContours: [],
    selectedRanges: [],
    selectedPaths: [],
    selectedModels: [],
    selectedVehicles: [],
    selectedJobs: [],

    isHelperActive: false, // header help button
    isPanicActive: false, // header p-panic button

    displayContourLayers: null, // contours layer, type number
    displayPathLevel: null, // path's property level, type number

    displayRangeLayers: null, // contours layer, type number

    isShowEnvelopeActive: false, // toolbar's button
    isShowTrafficActive: false, // toolbar's button
    isDrawContoursActive: false, // toolbar's button
    isDrawRangesActive: false, // toolbar's button
    isDrawPathsActive: false, // toolbar's button
    isMultiSelectActive: false, // toolbar's button
    isTrackActive: false, // toolbar's button
    isAltFnActive: false, // toolbar's button
    showJobsTab: false,
    isUserInfo: {},
    isDrawBezier: true,
    isAllData: false
};

const getters = {
    tableOpened: state => id => state.openedBoxes.indexOf(id) >= 0
};

const mutations = {
    [enums.CLEAR](state) {
        console.warn('[VUEX] status.clear!');
        state.selectedContours = [];
        state.selectedPaths = [];
        state.selectedModels = [];
        state.selectedVehicles = [];
    },

    [enums.OPEN_BOX](state, id) {
        if (!id) return;
        if (state.openedBoxes.indexOf(id) < 0) state.openedBoxes.push(id);
    },
    [enums.OPEN_BOX_SINGLETON](state, id) {
        if (!id) return;
        state.openedBoxes = [id];
    },
    [enums.CLOSE_BOX](state, id) {
        if (!id) return;
        let idx = state.openedBoxes.indexOf(id);
        if (idx >= 0) state.openedBoxes.splice(idx, 1);
    },
    [enums.TOGGLE_BOX](state, excludes) {
        if (!state.toggledBoxes.length && !state.openedBoxes.length) return;
        if (typeof excludes === 'string') excludes = [excludes];
        if (!state.toggledBoxes.length) {
            let res = state.openedBoxes;
            (excludes || []).forEach(e => {
                let idx = res.indexOf(e);
                if (idx >= 0) res.splice(idx, 1);
            });
            state.toggledBoxes = res;
            state.openedBoxes = excludes;
        } else {
            let res = state.toggledBoxes;
            (excludes || []).forEach(e => {
                let idx = res.indexOf(e);
                if (idx >= 0) res.splice(idx, 1);
            });
            state.openedBoxes = res;
            state.toggledBoxes = [];
        }
    },

    // [enums.UPDATE_MAP_GRID] (state, obj) {
    //     if (!obj) return;
    //     Vue.set(state, 'mapGrid', Object.assign({}, obj));
    // },

    // contours
    [enums.SELECT_CONTOURS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            if (state.selectedContours.indexOf(v) < 0) state.selectedContours.push(v);
        });
    },
    [enums.DESELECT_CONTOURS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            let idx = state.selectedContours.indexOf(v);
            if (idx >= 0) state.selectedContours.splice(idx, 1);
        });
    },
    [enums.DESELECT_CONTOURS_ALL](state, ids) {
        if (!ids || !ids.length) return (state.selectedContours = []);
        state.selectedContours = ids.toString().split(',');
    },

    // ranges
    [enums.SELECT_RANGES](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            if (state.selectedRanges.indexOf(v) < 0) state.selectedRanges.push(v);
        });
    },
    [enums.DESELECT_RANGES](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            let idx = state.selectedRanges.indexOf(v);
            if (idx >= 0) state.selectedRanges.splice(idx, 1);
        });
    },
    [enums.DESELECT_RANGES_ALL](state, ids) {
        if (!ids || !ids.length) return (state.selectedRanges = []);
        state.selectedRanges = ids.toString().split(',');
    },

    [enums.SELECT_PATHS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            if (state.selectedPaths.indexOf(v) < 0) state.selectedPaths.push(v);
        });
    },
    [enums.DESELECT_PATHS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            let idx = state.selectedPaths.indexOf(v);
            if (idx >= 0) state.selectedPaths.splice(idx, 1);
        });
    },
    [enums.DESELECT_PATHS_ALL](state, ids) {
        if (!ids || !ids.length) return (state.selectedPaths = []);
        state.selectedPaths = ids.toString().split(',');
    },

    [enums.SELECT_MODELS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            if (state.selectedModels.indexOf(v) < 0) state.selectedModels.push(v);
        });
    },
    [enums.DESELECT_MODELS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            let idx = state.selectedModels.indexOf(v);
            if (idx >= 0) state.selectedModels.splice(idx, 1);
        });
    },
    [enums.DESELECT_MODELS_ALL](state, ids) {
        if (!ids || !ids.length) return (state.selectedModels = []);
        state.selectedModels = ids.toString().split(',');
    },

    [enums.SELECT_VEHICLES](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            if (state.selectedVehicles.indexOf(v) < 0) state.selectedVehicles.push(v);
        });
    },
    [enums.DESELECT_VEHICLES](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            let idx = state.selectedVehicles.indexOf(v);
            if (idx >= 0) state.selectedVehicles.splice(idx, 1);
        });
    },
    [enums.DESELECT_VEHICLES_ALL](state, ids) {
        if (!ids || !ids.length) return (state.selectedVehicles = []);
        state.selectedVehicles = ids.toString().split(',');
    },

    [enums.SELECT_JOBS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            if (state.selectedJobs.indexOf(v) < 0) state.selectedJobs.push(v);
        });
    },
    [enums.DESELECT_JOBS](state, ids) {
        if (!ids || !ids.length) return;
        ids.forEach(v => {
            let idx = state.selectedJobs.indexOf(v);
            if (idx >= 0) state.selectedJobs.splice(idx, 1);
        });
    },
    [enums.DESELECT_JOBS_ALL](state, ids) {
        if (!ids || !ids.length) return (state.selectedJobs = []);
        state.selectedJobs = ids.toString().split(',');
    },

    [enums.DISPLAY_CONTOUR_LAYERS](state, layer) {
        state.displayContourLayers = layer;
    },
    [enums.DISPLAY_PATH_LEVEL](state, level) {
        state.displayPathLevel = level;
    },
    [enums.DISPLAY_RANGE_LAYERS](state, layer) {
        state.displayRangeLayers = layer;
    },

    [enums.ACTIVE_SHOW_ENVELOPE](state) {
        state.isShowEnvelopeActive = true;
    },
    [enums.DEACTIVE_SHOW_ENVELOPE](state) {
        state.isShowEnvelopeActive = false;
    },

    [enums.ACTIVE_SHOW_TRAFFIC](state) {
        state.isShowTrafficActive = true;
    },
    [enums.DEACTIVE_SHOW_TRAFFIC](state) {
        state.isShowTrafficActive = false;
    },

    [enums.ACTIVE_DRAW_CONTOURS](state) {
        state.isDrawContoursActive = true;
    },
    [enums.DEACTIVE_DRAW_CONTOURS](state) {
        state.isDrawContoursActive = false;
    },

    [enums.ACTIVE_DRAW_RANGES](state) {
        state.isDrawRangesActive = true;
    },
    [enums.DEACTIVE_DRAW_RANGES](state) {
        state.isDrawRangesActive = false;
    },

    [enums.ACTIVE_DRAW_PATHS](state) {
        state.isDrawPathsActive = true;
    },
    [enums.DEACTIVE_DRAW_PATHS](state) {
        state.isDrawPathsActive = false;
    },

    [enums.ACTIVE_MULTI_SELECT](state) {
        state.isMultiSelectActive = true;
    },
    [enums.DEACTIVE_MULTI_SELECT](state) {
        state.isMultiSelectActive = false;
    },

    [enums.ACTIVE_ALTFN](state) {
        state.isAltFnActive = true;
    },
    [enums.DEACTIVE_ALTFN](state) {
        state.isAltFnActive = false;
    },

    [enums.ACTIVE_TRACK](state) {
        state.isTrackActive = true;
    },
    [enums.DEACTIVE_TRACK](state) {
        state.isTrackActive = false;
    },

    [enums.ACTIVE_PANIC](state) {
        state.isPanicActive = true;
    },
    [enums.DEACTIVE_PANIC](state) {
        state.isPanicActive = false;
    },

    [enums.ACTIVE_HELPER](state) {
        state.isHelperActive = true;
    },
    [enums.DEACTIVE_HELPER](state) {
        state.isHelperActive = false;
    },

    [enums.ACTIVE_AIRPORT](state, userInfo) {
        state.isUserInfo = userInfo;
    },
    [enums.DEACTIVE_AIRPORT](state) {
        state.isUserInfo = {};
    },

    [enums.ACTIVE_BEZIER](state) {
        state.isDrawBezier = true;
    },
    [enums.DEACTIVE_BEZIER](state) {
        state.isDrawBezier = false;
    },
    [enums.ACTIVE_SHOW_JOBS](state) {
        state.showJobsTab = true;
    },
    [enums.DEACTIVE_SHOW_JOBS](state) {
        state.showJobsTab = false;
    },
    [enums.ACTIVE_ALL_DATA](state) {
        state.isAllData = true;
    }
};

const actions = {};

export default {
    state,
    getters,
    actions,
    mutations
};
